package com.zretc.blog.service;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.zretc.api.pojo.Comment;
import com.zretc.api.pojo.Reply;
import com.zretc.blog.dao.ReplyMapper;
import com.zretc.security.entity.LoginUser;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReplyService {
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
//    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//    LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//    Integer replierId = loginUser.getUser().getId();
    public void addReply(Reply reply){
        String key = "Reply" + reply.getCommentId();
        stringRedisTemplate.delete(key);
        Integer id = reply.getReplierId();
        Map<String,Object> map= new HashMap<>();
        map.put("userId",id);
        map.put("value",3);
        rocketMQTemplate.syncSend("credit", map);
        replyMapper.addReply(reply);
    };
    public List<Reply> getReplyByComment(Integer commentId){
        String key = "Reply" + commentId;
        Long size = stringRedisTemplate.opsForList().size(key);
        List<String> list_string;      //写入redis的表
        List<Reply> list;           //返回Comment对象的表
        if(size != 0){
            list_string = stringRedisTemplate.opsForList().range(key,0,size);
            list = list_string.stream().map(s -> {
                Reply reply = JSON.parseObject(s,Reply.class);
                return reply;
            }).collect(Collectors.toList());
            // System.out.println(1111);
            return list;//缓存中存在，直接返回
        }else {

            list = replyMapper.getReplyByComment(commentId);//缓存中不存在，从数据库读取
            if(list.isEmpty()){
                return null;
            }
            list_string = list.stream().map(reply -> {
                return JSON.toJSONString(reply);
            }).collect(Collectors.toList());
            //System.out.println(list_string);
            stringRedisTemplate.opsForList().rightPushAll(key, list_string);
            return list;
        }

    // List<Reply> list = replyMapper.getReplyByComment(commentId);

      //return list;
    }
    public ArrayList<Reply> getReplyByUser(Integer replierId){
        ArrayList<Reply> list = replyMapper.getReplyByUser(replierId);
        return list;
    }
    public void deleteReply(Integer id){
        Reply reply = replyMapper.getReplyById(id);
        Integer cid = reply.getCommentId();
        String key ="Reply" + String.valueOf(cid);
        Long size = stringRedisTemplate.opsForList().size(key);
        //ist<String> list_string = stringRedisTemplate.opsForList().range(key,0,size); //用list接受！！！！！！！！！！！
        //System.out.println(blogJson);
        if(size != 0){
            stringRedisTemplate.delete(key);
        }
        replyMapper.deleteReply(id);
    }
    public void setReplyStatus(Reply reply) {
        replyMapper.setReplyStatus(reply);
    }

    public void deleteReplyByComment(Integer commentId){
        replyMapper.deleteReplyByComment(commentId);
    }
    public void likeReply(Integer id){
        replyMapper.likeReply(id);
    }
    public Reply getReplyById(Integer id){
        Reply reply = replyMapper.getReplyById(id);
        return reply;
    }
}
